from selenium.webdriver.common.by import By
from selenium.webdriver.common.devtools.v135.css import CSSStyle
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from BlogAutoTest.common.Utils import BlogDriver


class BlogDetail:
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_login.html"
        self.driver=BlogDriver.driver
        self.driver.get(self.url)
        self.wait = WebDriverWait(self.driver,10)

    def BlogDetail(self):
        self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"#username"))).send_keys("zhangsan")
        self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"#password"))).send_keys("123456")
        self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#submit"))).click()
        self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > a"))).click()

        try:
            #检测文章标题
            self.wait.until(
                EC.presence_of_element_located((By.CSS_SELECTOR,"body > div.container > div.right > div > div.title"))
            )
            #检测文章上传时间
            self.wait.until(
                EC.presence_of_element_located((By.CSS_SELECTOR,"body > div.container > div.right > div > div.date"))
            )
            #检测文章内容
            self.wait.until(
                EC.presence_of_element_located((By.CSS_SELECTOR,"#detail"))
            )
            #点击“编辑”按钮
            self.wait.until(
                EC.element_to_be_clickable((By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(1)"))
            ).click()
            #检测是否进入编辑页
            self.wait.until(
                EC.presence_of_element_located((By.CSS_SELECTOR,"#title"))
            )
            self.driver.back()
            #点击“删除”按钮
            self.wait.until(
                EC.element_to_be_clickable((By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)"))
            ).click()
            self.wait.until(EC.alert_is_present())
            alert=self.driver.switch_to.alert
            print(f"弹窗提示：",alert.text)
            alert.dismiss()




        except Exception as e:
            print(f"BlogDetailTest 执行失败",e)
            BlogDriver.getScreeShot()
            raise



